<?php
	function feedAfterFollow($idFollowed,$typeOfFollowed,$idFollower,$dbConn){
		//$idFollowed la id cua nguoi hay shop duoc follow
		//$typeOfFollowed co 2 gia tri 0: nguoi duoc theo doi la shop, 1: nguoi duoc theo doi la user

		if($typeOfFollowed = 0) {
			$q = "SELECT id_product FROM product WHERE id_shop = $idFollowed created_date DESC LIMIT 5";
			//lay tat ca idproduct cua san pham cua shop moi vua duoc upload trong thoi gian gan day tu bang product de insert feed

			$r = exeQuery($q, $dbConn);
			while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)) {
				//dung loop de insert 5 san pham duoc upload vao feed
				$idFeed = substr(md5("$idFollowed-0-$idFollower-{$row['id_product']}"),0,20);
				$q =  "INSERT INTO feeds (id_feed,id_owner,type_owner,id_user_to,id_product,type_event,created_date)";
				$q .= " VALUES ($idFeed,$idFollowed,0,$idFollower,{$row['id_product']},0,NOW())";
			}
		} elseif($typeOfFollowed = 1){
			$q = "SELECT id_product FROM user_collected WHERE id_user = $idFollower ORDER BY created_date DESC LIMIT 5 ";
			//lay idproduct cua 5 san pham ma user duoc theo doi da collect trong thoi gian gan day de insert bang feed

			$r = exeQuery($q, $dbConn);
			while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC)) {
				//dung loop de insert 5 san pham do vao bang feed	

				$idFeed = substr(md5("$idFollowed-1-$idFollower-{$row['id_product']}"),0,20);

				$q =  "INSERT INTO feeds (id_feed,id_owner,type_owner,id_user_to,id_product,type_event,created_date)";
				$q .= " VALUES ($idFeed,$idFollowed,1,$idFollower,{$row['id_product']},0,NOW())"; //type_owner = 1 la cua user thuong

			}
		}
	}
?>